package com.ruoyi.quanzi.controller.app;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.quanzi.domain.SunMessage;
import com.ruoyi.quanzi.service.ISunMessageService;

/**
 * 消息管理Controller
 * 
 * @author ruoyi
 * @date 2023-01-15
 */
@RestController
@RequestMapping("/app/message")
public class AppMessageController extends BaseController
{
    @Autowired
    private ISunMessageService sunMessageService;

    /**
     * 查询消息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SunMessage sunMessage)
    {
        // 设置当前登录用户ID
        sunMessage.setUserId(getUserId());
        
        // 开始分页
        startPage();
        
        // 查询消息列表
        List<SunMessage> list = sunMessageService.selectSunMessageList(sunMessage);
        
        return getDataTable(list);
    }

    /**
     * 获取消息详细信息
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(sunMessageService.selectSunMessageById(id));
    }

    /**
     * 新增消息
     */
    @PostMapping
    public AjaxResult add(@RequestBody SunMessage sunMessage)
    {
        // 设置当前登录用户ID
        sunMessage.setUserId(getUserId());
        
        return toAjax(sunMessageService.insertSunMessage(sunMessage));
    }

    /**
     * 修改消息
     */
    @PutMapping
    public AjaxResult edit(@RequestBody SunMessage sunMessage)
    {
        // 设置当前登录用户ID
        sunMessage.setUserId(getUserId());
        
        return toAjax(sunMessageService.updateSunMessage(sunMessage));
    }

    /**
     * 删除消息
     */
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(sunMessageService.deleteSunMessageByIds(ids));
    }
    
    /**
     * 标记消息为已读
     */
    @PutMapping("/read/{id}")
    public AjaxResult markAsRead(@PathVariable("id") Long id)
    {
        SunMessage sunMessage = new SunMessage();
        sunMessage.setId(id);
        sunMessage.setIsRead(1);
        
        return toAjax(sunMessageService.updateSunMessage(sunMessage));
    }
    
    /**
     * 标记所有消息为已读
     */
    @PutMapping("/read/all")
    public AjaxResult markAllAsRead()
    {
        return toAjax(sunMessageService.markAllAsRead(getUserId()));
    }
    
    /**
     * 获取未读消息数量
     */
    @GetMapping("/unread/count")
    public AjaxResult getUnreadCount()
    {
        int count = sunMessageService.getUnreadCount(getUserId());
        return success(count);
    }
}